﻿using System;
using System.Collections.Generic;
using System.Text;
using WAD.Model;

namespace WAD.DAL
{
    public class DAOFCRecord : DAO
    {
        /// <summary>
        /// 添加金额交易记录
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static int Add(MFCRecord m)
        {
            if (string.IsNullOrEmpty(m.remark)) m.remark = "";
            return ExecuteNonQuery(
                string.Format("insert into fc_record (user_id,amount,balance,direction,orderid,articleid,ip,modelid,remark,date)values({0},{1},{2},{3},'{4}',{5},'{6}',{7},'{8}',getdate())",
                        m.user_id,
                        m.amount,
                        m.balance,
                        m.direction,
                        m.orderid,
                        m.article_id,
                        m.ip,
                        m.modelid,
                        m.remark
                        ));
        }
        /// <summary>
        /// 添加,并返回ID
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static int Add_Return(MFCRecord m)
        {
            if (string.IsNullOrEmpty(m.remark)) m.remark = "";
            object o = ExecuteScalar(
                string.Format("insert into fc_record (user_id,amount,balance,direction,orderid,articleid,ip,modelid,remark,date)values({0},{1},{2},{3},'{4}',{5},'{6}',{7},'{8}',getdate());select @@IDENTITY",
                        m.user_id,
                        m.amount,
                        m.balance,
                        m.direction,
                        m.orderid,
                        m.article_id,
                        m.ip,
                        m.modelid,
                        m.remark
                        ));

            if (o != null)
                return Convert.ToInt32(o);
            return 0;
        }

        public static int Delete(int id)
        {
            return ExecuteNonQuery("delete fc_record where id=" + id);
        }

        /// <summary>
        /// PC代理商后台：获取返利统计
        /// </summary>
        /// <param name="user_id"></param>
        /// <param name="modelid"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetTotal_FanLi(int agentid, int user_id, string nick_name, int modelid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select fc_record.user_id,sum(op_total*op_price) as ad_money,sum(amount) as fl_money from fc_record,fc_article where fc_record.articleid=fc_article.id  and fc_article.state=1");
            sb.AppendFormat(" and fc_record.user_id={0} and modelid={1}", agentid, modelid);
            //if (user_id > 0)
            //    sb.AppendFormat(" and fc_article.user_id={0}", user_id);
            sb.Append(" group by fc_record.user_id");
            return DAO.BindMap(sb.ToString());
        }
    }
}